package com.lemon.ui.codemaker.namecover;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.Toolkit;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.StringSelection;
import java.awt.datatransfer.Transferable;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;

import org.apache.commons.lang3.StringUtils;

import com.jgoodies.forms.layout.ColumnSpec;
import com.jgoodies.forms.layout.FormLayout;
import com.jgoodies.forms.layout.FormSpecs;
import com.jgoodies.forms.layout.RowSpec;

public class NameCoverWindow extends JDialog {

	/**
	 * 
	 */
	private static final long serialVersionUID = 3030377219533097114L;


	private final JPanel contentPanel = new JPanel();
	private JTextField textField;
	private JTextField textField_1;
	private Clipboard clipboard;

	/**
	 * Create the dialog.
	 */
	public NameCoverWindow() {
		setTitle("驼峰工具");
		setResizable(false);
		setModal(true);
		setType(Type.UTILITY);
		setBounds(100, 100, 453, 170);
		getContentPane().setLayout(new BorderLayout());
		contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
		getContentPane().add(contentPanel, BorderLayout.CENTER);
		contentPanel.setLayout(new FormLayout(new ColumnSpec[] {
				FormSpecs.RELATED_GAP_COLSPEC,
				FormSpecs.DEFAULT_COLSPEC,
				FormSpecs.RELATED_GAP_COLSPEC,
				FormSpecs.DEFAULT_COLSPEC,
				FormSpecs.RELATED_GAP_COLSPEC,
				ColumnSpec.decode("default:grow"),},
			new RowSpec[] {
				FormSpecs.RELATED_GAP_ROWSPEC,
				FormSpecs.DEFAULT_ROWSPEC,
				FormSpecs.RELATED_GAP_ROWSPEC,
				FormSpecs.DEFAULT_ROWSPEC,}));
		clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
		{
			JLabel lblNewLabel = new JLabel("待转字符串");
			contentPanel.add(lblNewLabel, "2, 2");
		}
		{
			textField = new JTextField();
			textField.addMouseListener(new MouseAdapter() {
				@Override
				public void mouseClicked(MouseEvent arg0) {
					pastFromClip();
					textField.selectAll();
				}
			});
			pastFromClip();
			contentPanel.add(textField, "6, 2, fill, default");
			textField.setColumns(10);
		}
		{
			JLabel lblNewLabel_1 = new JLabel("转换结果");
			contentPanel.add(lblNewLabel_1, "2, 4");
		}
		{
			textField_1 = new JTextField();
			contentPanel.add(textField_1, "6, 4, fill, default");
			textField_1.setColumns(10);
		}
		{
			JPanel buttonPane = new JPanel();
			buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
			getContentPane().add(buttonPane, BorderLayout.SOUTH);
			{
				JButton okButton = new JButton("转换");
				okButton.addActionListener(new ActionListener() {
					@Override
					public void actionPerformed(ActionEvent e) {
						if(StringUtils.isNotBlank(textField.getText())) {
							textField_1.setText(NameCover.autoCoverName(textField.getText()));
							Transferable tText = new StringSelection(textField_1.getText());
							clipboard.setContents(tText, null);
						}
					}
				});
				okButton.setActionCommand("OK");
				buttonPane.add(okButton);
				getRootPane().setDefaultButton(okButton);
			}
			{
				JButton cancelButton = new JButton("取消");
				cancelButton.addActionListener(new ActionListener() {
					@Override
					public void actionPerformed(ActionEvent e) {
						dispose();
					}
				});
				cancelButton.setActionCommand("Cancel");
				buttonPane.add(cancelButton);
			}
		}
	}
	
	private void pastFromClip() {
		Transferable clipT = clipboard.getContents(null);
		if(clipT != null && clipT.isDataFlavorSupported(DataFlavor.stringFlavor)) {
			try {
				textField.setText((String)clipT.getTransferData(DataFlavor.stringFlavor));
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}
	

}
